package DataStructures.Stack;

/**
 * @author ：ALi
 * @date ：Created in 2021/11/2 21:04
 * @description：个位数计算器
 * @modified By：
 * @version: $
 */
public class Calculator {

    public static void main(String[] args){
        String str = "7-6-1-8";

        int res = 0;
        int num1 = 0;
        int num2 = 0;
        int oper = 0;
        char ch = ' ';//扫描用的
        int index = 0;
        //拼接多位数
        String keepNum = "";

        ArrayStack numberStack = new ArrayStack(10);
        ArrayStack operStack = new ArrayStack(10);

        while (true){
            ch = str.charAt(index);
            if(isOper(ch)){
                //是操作符的时候执行下面的代码
                if(operStack.isEmpty()){
                    //符号栈为空直接进栈
                    operStack.push(ch);
                }else {
                    //如果当前的运算符比栈顶的运算符优先级低，则进行计算，并将结果放在numberStact中
                    //否则直接进栈
                    if (priority(ch) <= priority(operStack.peek())) {
                        num1 = numberStack.pop();
                        num2 = numberStack.pop();
                        oper = operStack.pop();
                        res = cal(num1, num2, oper);
                        numberStack.push(res);
                        //计算后将运算符放入符号栈operStack
                        // operStack.push(ch);
                        //上面的两行是直接将符号入栈了，存在bug：如计算:1-1*1+9

                        //此处不应该将运算符直接放进符号栈，需要再次进行判断，上面的if判断条件必须有=号，
                        // 否则就将-的优先级比+高才可以
                        index--;
                    } else {
                        operStack.push(ch);
                    }
                }
            }
            //是数字的时候
            else {
//                numberStack.push(ch);
                //改进为可以计算多位数

                keepNum+=ch;

                    if (index == str.length()-1){
                        numberStack.push(Integer.parseInt(keepNum));

                    }else {
                        //下一个是操作符时
                        if(isOper(str.charAt(index+1))){
                            numberStack.push(Integer.parseInt(keepNum));
                            keepNum="";
                        }
                    }

            }
            index++;
            if(index >= str.length()){
                break;
            }


        }

        //全部入栈后，计算最后的结果
        while (true){
            if(operStack.isEmpty()){
                break;
            }
            num1 = numberStack.pop();
            num2 = numberStack.pop();
            oper = (char)operStack.pop();
            res = cal(num1,num2,oper);
            numberStack.push(res);

        }

        //取出栈顶的元素，并且不弹出栈
        System.out.println(numberStack.peek());



    }
    //判断优先级的
    public static int priority(int oper){
        if(oper == '*' || oper == '/'){
            return 3;
        }
        //此处是为了将-与+的优先级分开
// else if (oper == '-'){
//            return 2;
//        }
 else {
            return 1;
        }
    }
    //判断是否为运算符
    public static boolean isOper(char val){
        boolean res = (val == '*' || val == '/' || val == '+' || val == '-');
        return res;
    }
    //进行计算并返回计算结果
    public static int cal(int num1,int num2,int oper){
        int res = 0;
        switch (oper){
            case '*':
                res = num1 * num2;
                break;
            case '/':
                res = num2 / num1;
            break;
            case '+':
                res = num1 + num2;
            break;
            case '-':
                res = num2 - num1;
            break;
            default:
                break;

        }
        return res;
    }

}